#include "EventManager.hpp"
#include "Action.hpp"

#include <OGRE\OgreLogManager.h>

void EventManager::postEvent(std::string eventName)
{
	for (int i = 0; i < m_Listener[eventName].size(); i++)
	{
		char msg[200];
		sprintf(msg, "posting event: %s", eventName.c_str());
		Ogre::LogManager::getSingleton().logMessage(msg);
		m_Listener[eventName].at(i)->process();
	}
}

void EventManager::registerListener(std::string eventName, Action* eventListener)
{
	char msg[200];
	sprintf(msg, "Registering listener on event: %s with action: %s", eventName.c_str(), eventListener->getName());
	Ogre::LogManager::getSingleton().logMessage(msg);
	eventListener->setOwner(this);
	m_Listener[eventName].push_back(eventListener);
}

void EventManager::clearEventListeners(std::string eventName)
{
	std::map<std::string, std::vector<Action*> >::iterator iter = m_Listener.find( eventName );

	if (iter != m_Listener.end())
	{
		for (int i = 0; i < iter->second.size(); i++)
			delete iter->second.at(0);
		iter->second.clear();
		m_Listener.erase( iter );
	} else
	{
		char msg[200];
		sprintf(msg, "Cannot remove event listeners for event  %s because it doesn't exist.", eventName.c_str());
		Ogre::LogManager::getSingleton().logMessage(msg);
	}
}
